﻿#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

namespace VRChatAvatarToolkit {
    public class AvatarAnimationPlayer : MoyuToolkitUtils {
        private Vector2 mainScrollPos;
        GameObject avatar;

        private void OnInspectorUpdate()
        {
            var newAvatar = Selection.activeGameObject;
            if (newAvatar != avatar)
            {
                avatar = newAvatar;
                Repaint();
            }
        }
        private void OnGUI()
        {
            mainScrollPos = GUILayout.BeginScrollView(mainScrollPos);
            GUILayout.Space(10);
            GUI.skin.label.fontSize = 24;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label("动作播放器");
            GUI.skin.label.fontSize = 12;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label("by:如梦");
            GUILayout.Space(10);
            GUI.skin.label.fontSize = 12;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label("简单地让模型动起来，注意只能在运行中使用");
            GUILayout.Space(10);

            var runFlag = Application.isPlaying;
            EditorGUILayout.HelpBox(runFlag ? "编辑模型前记得先停止运行哦" : "先点击下方的按钮运行场景吧~", MessageType.Info);
            GUILayout.Space(10);
            if (GUILayout.Button(runFlag ? "停止运行" : "运行场景"))
            {
                EditorApplication.ExecuteMenuItem("Edit/Play");
            }

            if (avatar == null)
            {
                GUILayout.Space(10);
                EditorGUILayout.HelpBox("先在场景中选择一个模型吧~", MessageType.Warning);
            }

            GUILayout.Space(10);
            var animList = new string[,] {
            { "行走1", "Walk1" }, { "行走2", "Walk2" }, { "跑步1", "Run1" },{ "躺下1", "Lie1" },
            { "T姿势", "TPose" }, { "", "" }, { "", "" }, { "", "" }
        };
            var index = 0;
            while (index < animList.Length / 2)
            {
                GUILayout.BeginHorizontal();
                for (var i = 0; i < 4; i++, index++)
                {
                    var key = animList[index, 0];
                    var val = animList[index, 1];
                    if (key != "" && GUILayout.Button(key))
                        PlayAnimation(avatar, val);
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.Space(10);
            GUILayout.EndScrollView();
        }

        private void PlayAnimation(GameObject avatar, string name)
        {
            if (avatar == null)
                return;
            if (!Application.isPlaying)
                return;
            var animator = avatar.GetOrAddComponent<Animator>();
            animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
            var animController = new AnimatorController();
            animController.AddLayer("action");
            animController.layers[0].stateMachine.AddState("action");
            var motion = AssetDatabase.LoadAssetAtPath<Motion>(GetScriptPath() + "AvatarAnimationPlayer/Animation/" + name + ".anim");
            animController.layers[0].stateMachine.states[0].state.motion = motion;
            animator.runtimeAnimatorController = animController;
            avatar.SetActive(true);
        }
    }
}
#endif